package com.wjf.accesscontrol.ui.main

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.wjf.accesscontrol.base.MVVMBaseFragment
import com.wjf.accesscontrol.databinding.FragmentMainBinding
import com.wjf.accesscontrol.entity.IdCardEntity
import com.wjf.accesscontrol.entity.SocialCardEntity
import com.wjf.accesscontrol.entity.StationDetailEntity
import com.wjf.accesscontrol.net.utils.Status
import com.wjf.accesscontrol.ui.TitleBarActivity
import com.wjf.accesscontrol.ui.read.dialog.DIALOG_TYPE_1
import com.wjf.accesscontrol.util.AppUtils
import com.wjf.accesscontrol.util.Constant
import com.wjf.accesscontrol.util.JUMP_TO_IdCardFragment
import com.wjf.accesscontrol.util.JUMP_TO_SocialFragment
import com.wjf.accesscontrol.util.LogUtils
import com.wjf.accesscontrol.util.sdkUtils.ReadCallback
import com.wjf.accesscontrol.util.sdkUtils.ReadCardUtils
import com.wjf.accesscontrol.util.TYPE_1
import com.wjf.accesscontrol.util.TYPE_2
import com.wjf.accesscontrol.util.checkMachineIdClick
import com.wjf.accesscontrol.util.handler.HandlerCallback
import com.wjf.accesscontrol.util.handler.HandlerUtil
import com.wjf.accesscontrol.util.sdkUtils.TelpoUtils

class MainFragment : MVVMBaseFragment<MainViewModel,FragmentMainBinding>(), HandlerCallback, ReadCallback {

    private var readId = true

    companion object{
        var handlerCallback: HandlerCallback? = null
    }

    override fun initViewModel() = ViewModelProviders.of(this).get(MainViewModel::class.java)

    override fun initViewBinding(inflater: LayoutInflater, container: ViewGroup?) =
        FragmentMainBinding.inflate(inflater,container,false)

    override fun initView(){}

    override fun initData() {
        ReadCardUtils.getInstance().init(activity)
        ReadCardUtils.getInstance().deviceConnect()
        TelpoUtils.setCommonUtil(mView.context)

        if (!TextUtils.isEmpty(Constant.machineId)){
            vm.stationDetail().observe(this, Observer {
                it?.let { resource ->
                    if (resource.status == Status.SUCCESS) {
                        if (resource.data is StationDetailEntity){
                            Constant.province = "${resource.data.province}000000"
                            Constant.city = "${resource.data.city}000000"
                            Constant.region = "${resource.data.region}000000"
                            Constant.stationId = "${resource.data.id}"
                            Constant.stationName = "${resource.data.name}"
                            (activity as TitleBarActivity).setStationName("${resource.data.name}")
                        }
                    }
                }
            })
        }
    }

    override fun initClick() {
        binding.ivIdCard.checkMachineIdClick {
            TitleBarActivity.newInstance(it.context,JUMP_TO_IdCardFragment)
        }

        binding.ivSocial.checkMachineIdClick {
            TitleBarActivity.newInstance(it.context,JUMP_TO_SocialFragment)
        }
    }

    override fun onResume() {
        super.onResume()
        handlerCallback = this
        if (!TextUtils.isEmpty(Constant.machineId)){
            HandlerUtil.instance.startHandler()
        }
        ReadCardUtils.getInstance().setReadCallback(this)
    }

    override fun onStop() {
        super.onStop()
        HandlerUtil.instance.clearMsg()
    }

    override fun onDestroy() {
        super.onDestroy()
        ReadCardUtils.getInstance().clearCallback()
        ReadCardUtils.getInstance().deviceDisconnect()
        handlerCallback = null
    }

    override fun handlerCallback() {
        LogUtils.d("__main-handlerCallback","1")
        if (!TextUtils.isEmpty(Constant.machineId)){
            HandlerUtil.instance.startHandler()
        }

        when(readId){

            true -> {
                ReadCardUtils.getInstance().readIdCard()
                readId = !readId
            }

            else -> {
                ReadCardUtils.getInstance().readSocial()
                readId = !readId
            }
        }

    }

    override fun idData(idCardEntity: IdCardEntity) {
        stationDetail(idCardEntity.idNumber, idCardEntity.name, idCardEntity.gender, TYPE_1)
    }

    override fun socialData(socialCardEntity: SocialCardEntity) {
        stationDetail(socialCardEntity.idCardNum, socialCardEntity.name, socialCardEntity.gender, TYPE_2)
    }

    override fun errMsg(msg: String?) {}



    private fun stationDetail(idNumber: String, name: String, gender: String, type: Int){

        LogUtils.d("__upload-1","1")
        if (AppUtils.repeatCommit(idNumber,System.currentTimeMillis(), type)){
            LogUtils.d("__upload-2","1")
            return
        }
        LogUtils.d("__upload-3","1")
        TelpoUtils.setRelayPowerOn()

        AppUtils.showResultDialog(fragmentManager = childFragmentManager, type = DIALOG_TYPE_1)

        vm.stationDetail().observe(this, Observer {
            it?.let { resource ->
                if (resource.status == Status.SUCCESS) {
                    if (resource.data is StationDetailEntity){
                        Constant.province = "${resource.data.province}000000"
                        Constant.city = "${resource.data.city}000000"
                        Constant.region = "${resource.data.region}000000"
                        Constant.stationId = "${resource.data.id}"
                        Constant.stationName = "${resource.data.name}"
                        upload(idNumber, name, gender, type)
                    }
                }
            }
        })
    }

    fun upload(idNumber: String, name: String, gender: String, type: Int){

        vm.upload(
            idNum = idNumber,
            name = name,
            sex = gender,
            accessType = type
        ).observe(this, Observer {
            when(it.status){
                Status.SUCCESS -> {
                }
                Status.ERROR -> {
                }
            }
        })
    }
}